﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace GoogleLanguage.WebControls
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:Literal runat=server></{0}:Literal>")]
    public class Literal : Panel
    {
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Text
        {
            get
            {
                if (ViewState["Text"] == null)
                {
                    ViewState["Text"] = String.Empty;
                }
                return (string)ViewState["Text"];
            }
            set
            {
                ViewState["Text"] = value;
            }
        }        

        [Bindable(true)]
        [Category("Appearance")]
        public Language DestinationLanguage
        {
            get
            {
                if (ViewState["DestinationLanguage"] == null)
                {
                    ViewState["DestinationLanguage"] = LanguageMapping.DEFAULT_LANGUAGE;
                }
                return (Language)ViewState["DestinationLanguage"];
            }
            set
            {
                ViewState["DestinationLanguage"] = value;
            }
        }

        protected override void RenderContents(HtmlTextWriter writer)
        {
            base.RenderContents(writer);
            writer.Write(Text);
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            string scriptKey = String.Format("Literal_{0}", this.ClientID);
            if (!Page.ClientScript.IsStartupScriptRegistered(scriptKey))
            {
                StringBuilder script = new StringBuilder();
                script.Append(String.Format("GoogleLanguageInitializeLiteral(\"{0}\", \"{1}\");", this.ClientID, LanguageMapping.GetLanguageCode(DestinationLanguage)));
                Page.ClientScript.RegisterStartupScript(this.GetType(), scriptKey, script.ToString(), true);
            }
        }
    }
}
